var t = !1, a = !1;
Page({
    data: {
      page:1,
      keyword:'',
      list:{},
      goodsList:[]
    },
    onLoad: function(options) {
      var a = this;
      a.goodList();
    },
    goodList: function () {
    
      var t = this;
      a = !1, t.setData({
        page: 1,
        list: []
      });
      var i = t.data.page || 1;
      
      getApp().request({
        url: getApp().api.default.goods_list1,
        data: {
          keyword:t.data.keyword,
          page: i
        },
        success: function (e) {
          0 == e.code && (0 == e.data.list.length && (a = !0), t.setData({
            page: i + 1
          }), t.setData({
            list: e.data.list,
            goodsList: e.data.purchaseGoods
          }));
        },
        complete: function () { }
      });
    },
    ConfirmFunc(e) {
        console.info(e)
    },
  inputConfirm: function (t) {
    var a = this, e = t.detail.value;
    0 != e.length && (a.setData({
      page: 1,
      keyword: e
    }),  a.goodList());
  },
  addGoods:function(g){
    var a = this ;
    var goods_id = g.currentTarget.dataset.gid;
    var goodsList = a.data.goodsList;
    if(goods_id){
      var is_true = false;
      for (var i = 0; i < goodsList.length; i++) {
        if (goodsList[i].indexOf(goods_id) >= 0) {
          is_true = true;
          wx.showToast({
            title: '已经添加了'
          });
        }
      }
      if(!is_true){
        goodsList.push(goods_id)
        var list = a.data.list;
        for (var x = 0; x < list.length; x++) {
          if (list[x].id == goods_id){
              list[x].purchase_true =1;
          }
        }
        this.setData({
          goodsList: goodsList,
          list:list
        });
        a.add('添加成功');
      }
    }
    


  },
   delGoods: function (g) {
    var a = this;
    
    var goods_id = g.currentTarget.dataset.gid;
    var goodsList = a.data.goodsList;
    if (goods_id) {
      var index = goodsList.indexOf(goods_id);
      if (index > -1) {
        goodsList.splice(index, 1);
        var list = a.data.list;
        for (var x = 0; x < list.length; x++) {
          if (list[x].id == goods_id) {
            list[x].purchase_true = 0;
          }
        }
        this.setData({
          goodsList: goodsList,
          list: list
        })
        a.add('移除成功');
      }
    }
  


  },
  add:function(msg){
    var a= this;
    var goodsList = a.data.goodsList;
    if(goodsList){
      getApp().request({
        url: getApp().api.default.purchase_goods_add,
        method: "post",
        data: {
          goods_list: goodsList
        },
        success: function (t) {
          getApp().core.hideLoading(), 0 == t.code && getApp().core.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            success: function (e) {
              getApp().core.redirectTo({
                url: "/pages/user/user"
              })
            }
          }), 1 == t.code && wx.showToast({
            title: msg
          });
        }
      });
     
    }
  }

})